﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmLyTam : Form
    {
        public FrmLyTam()
        {
            InitializeComponent();
        }

        private void FrmLyTam_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void Option1_CheckedChanged(object sender, EventArgs e)
        {
            if (Option1.Checked)
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Lytam1b;
            }
        }

        private void Option2_CheckedChanged(object sender, EventArgs e)
        {
            if (Option2.Checked)
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Lytam2b;
            }   

        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Lytam1a;
            }
            else
            {
                if (Option1.Checked)
                {
                    Option1_CheckedChanged(sender, e);
                }
                else
                {
                    Option2_CheckedChanged(sender, e);
                }
            }
            
        }

        private void FrmLyTam_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(618, 544);
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrLytam prLytam = new PrLytam();
            prLytam.Show();
            this.Hide();
        }

        private void FrmLyTam_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }

        //Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            string[] Var1 = regKey.GetValue("KtThan").ToString().Split('\t');
            Values = txtDaymongMin .Text ;//0
            Values = Values +"\t"+ Var1[1] ;//1
            Values = Values +"\t"+ txtLopdem .Text ;//2
            Values = Values +"\t"+ txtCachkhoangday .Text ;//3
            Values = Values +"\t"+ txtSoday .Text ;//4
            Values = Values +"\t"+ txtLoaicong .Text ;//5
            Values = Values +"\t"+ txtDayBt .Text ;//6
            Values = Values +"\t"+ txtDayong .Text ;//7
            Values = Values +"\t"+ txtKhaudien .Text ;//8
            Values = Values +"\t"+ txtCaocong .Text ;//9
            Values = Values +"\t"+ txtDayban .Text ;//10
            Values = Values +"\t"+ Var1[11] ;//11
            Values = Values +"\t"+ txtGocmo .Text ;//12
            Values = Values +"\t"+ txtMomong .Text ;//13
            Values = Values +"\t"+ Option1.Checked.ToString() ;//14
            Values = Values +"\t"+ txtLopdemm .Text ;//15
            string Values1;
            string[] Var2 = regKey.GetValue("Vatlieu").ToString().Split('\t');
            Values1 = Var2[0];
            Values1 = Values1 +"\t"+ Var2[1];
            Values1 = Values1 +"\t"+ Var2[2];
            Values1 = Values1 +"\t"+ Var2[3];
            Values1 = Values1 +"\t"+ Var2[4];
            Values1 = Values1 +"\t"+ Var2[5];
            Values1 = Values1 +"\t"+ Var2[6];
            Values1 = Values1 +"\t"+ Var2[7];
            Values1 = Values1 +"\t"+ Var2[8];
            Values1 = Values1 + "\t" + txtVlMong.Text;
            Values1 = Values1 +"\t"+ Var2[10];

            regKey.SetValue("KtThan",Values);
            regKey.SetValue("Vatlieu", Values1);
            regKey.SetValue("GlobalLytrinh", txtLytrinh.Text);
            regKey.Close();
        }
        //Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtThan").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                txtDaymongMin.Text=STR[i++];//0
                i++;
                // Var(1) .Text ;//1
                txtLopdem.Text = STR[i++];//2
                txtCachkhoangday.Text = STR[i++];//3
                txtSoday.Text = STR[i++];//4
                txtLoaicong.Text = STR[i++];//5
                txtDayBt.Text = STR[i++];//6
                txtDayong.Text = STR[i++];//7
                txtKhaudien.Text = STR[i++];//8
                txtCaocong.Text = STR[i++];//9
                txtDayban.Text = STR[i++];//10
                i++;
               // Var(11).Text;//11
                txtGocmo.Text = STR[i++];//12
                txtMomong.Text = STR[i++];//13
                Option1.Checked = bool.Parse(STR[i++]);//14
                txtLopdemm.Text = STR[i++];//15
                regKey.Close();
            }
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void FrmLyTam_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
